<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/css/layui.css"   media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/layuimini.css?v=2.0.4.2" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/themes/default.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/public.css" media="all">

    <%--添加layui-dtree样式--%>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui_ext/dtree/font/dtreefont.css">

</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-row">
            <%-- 左侧菜单树 --%>
            <div class="layui-col-md2">
                <!-- 树节点容器开始 -->
                <ul id="menuTree" class="dtree" data-id="0" style="width: 240px;"></ul>
                <!-- 树节点容器结束 -->
            </div>

            <%-- 右侧数据表格 --%>
            <div class="layui-col-md10">

                <%-- 表格工具栏 --%>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加 </button>
                    </div>
                </script>

                <%-- 数据表格 --%>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                <%-- 行工具栏 --%>
                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-close"></i>删除</a>
                </script>

                <%-- 添加和修改窗口 --%>
                <div style="display: none;padding: 5px" id="addOrUpdateWindow">
                    <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
                        <%-- 菜单编号 --%>
                        <input type="hidden" name="id">
                        <div class="layui-form-item">
                            <label class="layui-form-label">父级菜单</label>
                            <div class="layui-input-block">
                                <input type="hidden" name="pid" id="pid">
                                <ul id="menuSelectTree" class="dtree" data-id="0"></ul>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入菜单名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单地址</label>
                            <div class="layui-input-block">
                                <input type="text" name="href"  autocomplete="off" placeholder="请输入菜单地址" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单图标</label>
                            <div class="layui-input-block">
                                <%--添加图标隐藏域，保存选中的图标ID--%>
                                <input type="hidden" name="icon" id="icon">
                                <input type="text" name="iconFa" id="iconPicker" lay-filter="iconPicker" autocomplete="off" placeholder="请输入菜单图标" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">是否展开</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="spread" value="1" title="是">
                                    <input type="radio" name="spread" value="0" title="否" checked>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item layui-row layui-col-xs12">
                            <div class="layui-input-block" style="text-align: center;">
                                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span class="layui-icon layui-icon-add-1"></span>提交</button>
                                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>清空</button>
                                <button type="button" class="layui-btn layui-btn-danger" id="resetMenu"><span class="layui-icon layui-icon-return"></span>重置菜单</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

</body>

<%-- 导入layui的js文件--%>
<script src="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.extend({
        dtree: "${pageContext.request.contextPath}/statics/layui_ext/dtree/dtree",
        iconPickerFa: "${pageContext.request.contextPath}/statics/layui/js/lay-module/iconPicker/iconPickerFa"
    }).use(['form','jquery','table','layer','dtree','iconPickerFa'],function (){
        var form=layui.form;
        var $=layui.jquery;
        var table=layui.table;
        var layer=layui.layer;
        var dtree=layui.dtree;
        var iconPickerFa=layui.iconPickerFa
        //初始化左侧菜单树
        var menuTree=dtree.render({
            elem:"#menuTree",
            url:"/admin/menu/loadMenuTree",//请求地址
            dataStyle:"layuiStyle",//使用layui风格的数据格式
            dataFormat:"list",
            response:{message:"msg",statusCode:0}
        })
        //初始化渲染数据表格
        var tableIns=table.render({
            elem:'#currentTableId',
            url:'${pageContext.request.contextPath}/admin/menu/list',
            toolbar:'#toolbarDemo',
            cols:[[
                {field:'id',width:100,title:'菜单编号',align:'center'},
                {field:'title',width:280,title:'菜单名称',align:'center'},
                {field:'href',minWidth:200,title:'菜单地址',align:'center'},
                {field:'spread',width:100,title:'是否展开',align:'center',templet:function(d){
                   return  d.spread==1 ? "<font color='red'>是</font>" : "<font color='blue'>否</font>"
                }},
                {field:'icon',width:100,title:'菜单图标',align:'center',templet:function(d){
                    return "<i class='"+d.icon+"'></i>"
                }},
                {width:200,title:'操作',toolbar:'#currentTableBar',align:'center'},
            ]],
            page:true,
            done:function (res,curr,count){
                //判断当前页码师傅是大于1并且当前页码的数据量为0
                if(curr>1&&res.data.length==0){
                    var pageValue=curr-1;
                    tableIns.reload({
                        page:{curr:pageValue}
                    })
                }
            }
        });

        //渲染父级菜单下拉树组件
        var menuSelectTree=dtree.renderSelect({
            elem:'#menuSelectTree',
            url:'/admin/menu/loadMenuTree',
            dataStyle: "layuiStyle",
            dataFormat: "list",
            response: {message:"msg",statusCode: 0}
        })


        //渲染 icon 图标选择器组件
        iconPickerFa.render({
            elem:'#iconPicker',
            type:'fontClass',
            url:'/statics/layui/lib/font-awesome-4.7.0/less/variables.less',
            search:true,
            page:true,
            limit:12,
            click:function(data){
                $("#icon").val("fa "+data.icon)
            },
            success:function(d){
                console.log(d)
            }
        })

        //监听左边菜单树的节点点击事件
        dtree.on("node(menuTree)",function(obj){
            console.log(obj)
            tableIns.reload({
                where:{
                    "id":obj.param.nodeId
                },
                page:{curr:1}
            })
        })
        //监听下拉菜单树节点选中事件
        dtree.on("node(menuSelectTree)",function(obj){
            //给父级菜单的隐藏域赋值
            $("#pid").val(obj.param.nodeId);
        })
        //监听头部工具栏添加按钮事件
        table.on("toolbar(currentTableFilter)",function(obj){
            switch (obj.event){
                case "add"://添加按钮
                    openAddWindow();
                    break;
            }
        })
        //监听行内工具栏事件 update delete
        table.on("tool(currentTableFilter)",function(obj){
            switch (obj.event){
                case "edit":
                    openUpdateWindow(obj.data);
                    break;
                case "delete":
                    deleteById(obj.data);
                    break;
            }
        })
        //监听表单提交事件
        form.on("submit(doSubmit)",function(data){
            $.post(url,data.field,function (res) {
                if(res.success){
                    tableIns.reload();
                    layer.close(mainIndex);
                    //刷新所在菜单树
                    menuTree.reload();
                    //刷新选择树
                    menuSelectTree.reload();
                    layer.msg(res.message)
                }
            },"json")
            //禁止页面刷新
            return false;
        })
        var url,mainIndex
        //打开添加按钮弹窗
        function openAddWindow(){
            mainIndex=layer.open({
                type:1,
                title:'添加菜单',
                content:$("#addOrUpdateWindow"),
                area:['800px','500px'],
                success:function(res){
                    console.log(res)
                    $("#dataFrm")[0].reset();
                    url="/admin/menu/addMenu"
                }
            })
        }
        //打开修改对话框
        function openUpdateWindow(data){
            console.log('修改菜单',data)
            mainIndex=layer.open({
                type:1,
                title:'修改菜单',
                content:$("#addOrUpdateWindow"),
                area: ['800px','500px'],
                success:function(res){
                    form.val("dataFrm",data);
                    url="/admin/menu/updateMenu"
                    //图标选择器回显选中的图标
                    iconPickerFa.checkIcon("iconPicker",data.icon);
                    //父级别菜单回显
                    dtree.dataInit("menuSelectTree",data.pid);//参数1：下拉菜单树的id属性值，参数2父亲节点id值
                    dtree.selectVal("menuSelectTree");//参数：下拉菜单树的id属性值
                    //判断当前选中节点是否是一级菜单，如果是，那么该菜单没有父级菜单，此时应该显示请选择
                    if(data.pid==0){
                        //刷新菜单树
                        menuSelectTree.reload();
                    }
                }
            })
        }
        //删除菜单方法
        function deleteById(data){
           $.get("/admin/menu/checkMenuHasChild",{"id":data.id},function(res){
               if(res.exist){
                   layer.msg(res.message)
               }else{
                   layer.confirm("确定要删除[<font color='red'>"+data.title+"</font>]吗",{icon:3,title:'提示'},function(index){
                       $.post("/admin/menu/deleteById",{"id":data.id},function(result){
                           if(result.success){
                               tableIns.reload();
                               menuTree.reload();
                               menuSelectTree.reload();
                           }
                           layer.msg(result.message);
                       },"json");
                       layer.close(index);
                   });
               }
           },"json")
        }
    })
</script>
</html>